package utils;

import Book.Book;
import com.bit.utils.FileUtils;

import java.io.IOException;
import java.time.LocalDate;

public class AnalyzingBook {
    public void storeObject(Book[] book, String filename) throws IOException {
        int bookUselen = 0;
        for (int i = 0; i < book.length; i++) {
            if (book[i] != null){
                bookUselen++;
            }
        }
        StringBuilder jsonArray = new StringBuilder();
        for (int i = 0; i < bookUselen; i++) {
            if (book[i] != null){
                jsonArray.append(book[i].toJson());
            }
            if (i != bookUselen - 1){
                jsonArray.append("\n");
            }
        }
        FileUtils.writeFile(filename, jsonArray.toString());
    }

    public Book[] loadObject(String filename) throws IOException {
        return null;
    }

    private Book parseBookJson(String json) {
        return null;
    }

    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("西游记", "张三", "1000", LocalDate.of(2020, 1, 1));
        books[1] = new Book("红楼梦", "李四", "2000", LocalDate.of(2030, 1, 1));
        books[2] = new Book("水浒传", "王五", "3000", LocalDate.of(2040, 1, 1));
        books[3] = new Book("三国演义", "赵六", "4000", LocalDate.of(2050, 1, 1));
        AnalyzingBook analyzingBook = new AnalyzingBook();
        try {
            analyzingBook.storeObject(books, "allbooks.txt");
//            Book[] cur = analyzingBook.loadObject("Allbooks.txt");
//            for (int i = 0; i < cur.length; i++) {
//                System.out.println(cur[i]);
//            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
